package com.sxz.sbs.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.sxz.sbs.R;

/**
 * 通用对话框
 * @author tian
 */

public class StallAlertDialog extends Dialog {

    private Button btSure;
    private TextView tvContent;
    private Button btCancel;
    private TextView tvTitles;
    private LinearLayout llTitle;
    private View.OnClickListener onDefaultClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            cancel();
        }

    };
    private OnPositiveListener onPositiveListener;
    private OnNegativeListener onNegativeListener;
   // private View.OnClickListener onNegativeListener = onDefaultClickListener;
    private String mTitle;
    private String mMessage;
    private String positiveButtonText;
    private String negativeButtonText;
    private View mCustomView;
    private boolean isNegativeBtnShow=true;
    private Context context;
    private boolean showTitle = true;
    private StallAlertDialog(Context context) {
        super(context, R.style.Theme_Dialog_From_Bottom);
        this.context=context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_reminder_layout);
        tvTitles = (TextView) findViewById(R.id.tv_title);
        llTitle = (LinearLayout) findViewById(R.id.ll_title);
        btCancel = (Button) findViewById(R.id.tv_cancle);
        tvContent = (TextView)findViewById(R.id.tv_content);
        btSure = (Button) findViewById(R.id.tv_sure);
    }

    @Override
    public void show() {
        super.show();
        show(this);
    }

    /**
     * 显示
     * @param mDialog
     */
    private void show(StallAlertDialog mDialog) {

        if (mDialog.showTitle) {
            if (!TextUtils.isEmpty(mDialog.mTitle)) {
                mDialog.tvTitles.setText(mDialog.mTitle);
            }
        } else {
            mDialog.llTitle.setVisibility(View.GONE);
        }
        if (!TextUtils.isEmpty(mDialog.mMessage)) {
            mDialog.tvContent.setText(mDialog.mMessage);
        }
        if (!mDialog.isNegativeBtnShow) {
            mDialog.btCancel.setVisibility(View.GONE);
        } else {
            mDialog.btCancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    cancel();
                    if (onNegativeListener != null) {
                        onNegativeListener.onNegativeClick(v);
                    }
                }
            });
            if (!TextUtils.isEmpty(mDialog.negativeButtonText)) {
                mDialog.btCancel.setText(mDialog.negativeButtonText);
            }
        }
        mDialog.btSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cancel();
                if (onPositiveListener != null) {
                    onPositiveListener.onPositiveClick(v);
                }
            }
        });
        if (!TextUtils.isEmpty(mDialog.positiveButtonText)) {
            mDialog.btSure.setText(mDialog.positiveButtonText);
        }
    }

    public static class Builder {
        private StallAlertDialog mDialog;

        public Builder(Context context) {
            mDialog=new StallAlertDialog(context);
        }

        /**
         * 弹框标题
         * @param mDialogTitle 默认为提示
         * @return
         */
        public Builder setTitle(@NonNull String mDialogTitle) {
            mDialog.mTitle=mDialogTitle;
            return this;
        }
        /**
         * 是否显示标题
         * @param showTitle 默认为true
         * @return
         */
        public Builder showTitle(boolean showTitle) {
            mDialog.showTitle = showTitle;
            return this;
        }
        /***
         * 弹框内容
         * @param mDialogContent
         * @return
         */
        public Builder setMessage(@NonNull String mDialogContent) {
            mDialog.mMessage = mDialogContent;
            return this;
        }
        /**
         * 设置确认按钮的回调
         *
         * @param onPositiveListener
         */
        public Builder setPositiveButton(OnPositiveListener onPositiveListener) {
            if (onPositiveListener!=null){
                mDialog.onPositiveListener = onPositiveListener;
            }
            return this;
        }
        /**
         * 设置确认按钮的回调 自定义按钮文本
         *
         */
        public Builder setPositiveButton(String btSureText, OnPositiveListener onPositiveListener) {
            mDialog.positiveButtonText = btSureText;
            if (onPositiveListener!=null){
                mDialog.onPositiveListener = onPositiveListener;
            }
            return this;
        }
        /**
         * 设置确认按钮字体
         *
         */
        public Builder setPositiveButtonText(String btSureText) {
            mDialog.positiveButtonText = btSureText;
            return this;
        }
        /**
         * 设置取消按钮的回掉    默认按钮文本
         *
         * @param onNegativeListener
         */
        public Builder setNegativeButton(OnNegativeListener onNegativeListener) {
            if (onNegativeListener != null){
                mDialog.onNegativeListener = onNegativeListener;
            }
            return this;
        }

        /**
         * 设置取消按钮的回调  自定义按钮文本
         *
         * @param banCancelText,onNegativeListener
         */
        public Builder setNegativeButton(String banCancelText, OnNegativeListener onNegativeListener) {
            mDialog.negativeButtonText = banCancelText;
            if (onNegativeListener!=null){
                mDialog.onNegativeListener = onNegativeListener;
            }
            return this;
        }
        /**
         * 设置自定义内容View
         *
         * @param view
         */
        public Builder setCustomView(View view) {
            mDialog.mCustomView = view;
            return this;
        }
        /**
         * 设置该对话框能否被Cancel掉，默认可以
         *
         * @param cancelable
         */
        public Builder setCancelable(boolean cancelable) {
            mDialog.setCancelable(cancelable);
            return this;
        }
        /**
         * 设置该对话框能否被Cancel掉，默认可以
         *
         * @param cancelable
         */
        public Builder setCanceledOnTouchOutside(boolean cancelable) {
            mDialog.setCanceledOnTouchOutside(cancelable);
            return this;
        }
        /**
         * 设置对话框被cancel对应的回调接口，cancel()方法被调用时才会回调该接口
         *
         * @param onCancelListener
         */
        public Builder setOnCancelListener(OnCancelListener onCancelListener) {
            mDialog.setOnCancelListener(onCancelListener);
            return this;
        }

        /**
         * 设置对话框消失对应的回调接口，一切对话框消失都会回调该接口
         *
         * @param onDismissListener
         */
        public Builder setOnDismissListener(OnDismissListener onDismissListener) {
            mDialog.setOnDismissListener(onDismissListener);
            return this;
        }
        /**
         * 是否显示取消按钮
         *
         * @param isShowCancelButton 默认为true
         */
        public Builder setIsShowCanceButton(boolean isShowCancelButton) {
            mDialog.isNegativeBtnShow = isShowCancelButton;
            return this;
        }
        /**
         * 通过Builder类设置完属性后构造对话框的方法
         */
        public StallAlertDialog create() {
            return mDialog;
        }
    }
    public interface OnPositiveListener {
        void onPositiveClick(View view);
    }
    public interface OnNegativeListener {
        void onNegativeClick(View view);
    }

}

